<template>
  <div class="app-container">
    <el-button size="mini" type="primary" @click="addArticle">新增文章</el-button>
    <el-table
      v-loading="listLoading"
      size="mini"
      :data="list"
      style="width: 100%"
    >
      <el-table-column
        type="index"
      />
      <el-table-column
        prop="title"
        label="文章标题"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
      />
      <el-table-column
        prop="createPerson"
        label="创建人"
      />
      <el-table-column
        align="right"
      >
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"
          />
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >Edit</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="divid-page-class">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="pageTotal"
        @current-change="changePage"
      />
    </div>
    <div>
      <el-dialog :visible.sync="articleModDialog" :title="dialogTitle" width="60%">
        <div>
          <el-form ref="form" :model="articleForm" label-width="80px">
            <el-form-item label="文章标题">
              <el-input v-model="articleForm.title" />
            </el-form-item>
            <el-form-item label="文章内容">
              <div v-show="true" id="demo1" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="commitArticleInfo">立即创建</el-button>
              <el-button @click="articleModDialog = false">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article'
import articleMixin from '@/mixin/articleMixin'
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  mixins: [articleMixin],
  data() {
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      pageTotal: 1,
      articleModDialog: false,
      dialogTitle: '',
      articleForm: {},
      editor: null,
      editorData: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const params = {
        'currentPage': this.currentPage,
        'pageSize': this.pageSize
      }
      getArticleList(params).then(res => {
        this.list = res.data.records
        this.pageTotal = res.data.total
        this.listLoading = false
      })
    },
    addArticle() {
      this.addArticleForTemplate()
    },
    tranHtmltoFont(val) {
      const font = val.replace(/<[^>]*>/g, '')
      if (font.length > 30) {
        return font.slice(0, 30) + '...'
      }
      return font
    },
    changePage(val) {
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>
